<template>
  <el-dialog
    :title="title"
    :width="width"
    top="50px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form ref="form" :model="actReModel" :rules="rules" label-position="right" label-width="100px">
      <el-form-item :label="'流程名称'" prop="name">
        <el-input v-model="actReModel.name" :readonly="actReModel.id === '' ? false : 'readonly'" />
      </el-form-item>
      <el-form-item :label="'流程关键字'" prop="key">
        <el-input v-model="actReModel.key" />
      </el-form-item>
      <el-form-item :label="'版本号'" prop="version">
        <el-input v-model="actReModel.version" />
      </el-form-item>
      <el-form-item :label="'流程类型'" prop="category">
        <el-input v-model="actReModel.category" />
      </el-form-item>
      <el-form-item :label="'流程描述'" prop="description">
        <el-input v-model="actReModel.description" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" plain :loading="buttonLoading" @click="isVisible = false">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" plain :loading="buttonLoading" @click="submitForm">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { validMobile } from '@/utils/my-validate';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'ModelEdit',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      actReModel: this.initUser(),
      buttonLoading: false,
      screenWidth: 0,
      width: this.initWidth(),
      depts: [],
      roles: [],
      rules: {
        name: [
          {
            required: true,
            message: this.$t('rules.require'),
            trigger: 'blur'
          },
          {
            min: 4,
            max: 10,
            message: this.$t('rules.range4to10'),
            trigger: 'blur'
          }
        ],
        key: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ],
        version: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ],
        category: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ],
        description: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ]
      }
    };
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible;
      },
      set() {
        this.close();
        this.reset();
      }
    }
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth();
      })();
    };
  },
  methods: {
    initUser() {
      return {
        id: '',
        name: '',
        key: '',
        version: '',
        category: '',
        description: ''
      };
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth;
      if (this.screenWidth < 991) {
        return '90%';
      } else if (this.screenWidth < 1400) {
        return '45%';
      } else {
        return '800px';
      }
    },
    setUser(val) {
      this.actReModel = { ...val };
      console.log(this.actReModel)
    },
    close() {
      this.$emit('close');
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (!this.actReModel.id) {
            // create
            this.$post('activiti/actReModel', { ...this.actReModel }).then(
              () => {
                this.buttonLoading = false;
                this.isVisible = false;
                this.$message({
                  message: this.$t('tips.createSuccess'),
                  type: 'success'
                });
                this.$emit('success');
              }
            );
          } else {
            // update
            var metaInfoTmp = JSON.parse(this.actReModel.metaInfo);
            metaInfoTmp.description = this.actReModel.description;
            this.actReModel.metaInfo = JSON.stringify(metaInfoTmp);
            this.$put('activiti/actReModel', { ...this.actReModel }).then(
              () => {
                this.buttonLoading = false;
                this.isVisible = false;
                this.$message({
                  message: this.$t('tips.updateSuccess'),
                  type: 'success'
                });
                this.$emit('success');
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.form.clearValidate();
      this.$refs.form.resetFields();
      this.actReModel = this.initUser();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
